Imports System
Imports System.Data
Imports System.Collections
Imports System.Collections.Generic
Imports Cdm
Imports Cdm.AppServices
Imports Cdm.AppServices.Log
Imports Cdm.DataCore
Imports Cdm.DataCore.Custom

Partial Class Search_AdvancedSearchResults
    Inherits System.Web.UI.Page

#Region "CONSTANTS"
    Private Const _CLASS_NAME As String = "Search_AdvancedSearchResults"
#End Region

#Region "PRIVATE MEMBERS"
    Private _parameterCollection As Hashtable
    Private _sqlQuery As String
    Private _sortBy As String
#End Region

#Region "PRIVATE METHODS"

    Private Function CreateSqlQuery() As String

        Dim sqlString As String = ""

        Try

            sqlString = "EXECUTE spSearchFiles "

            For Each parameter As DictionaryEntry In _parameterCollection
                If parameter.Key = "@SortBy" Then

                    Me._sortBy = parameter.Value.ToString()

                Else

                    If parameter.Value.Equals(System.DBNull.Value) Or parameter.Value.Equals(String.Empty) Then
                        sqlString = sqlString & parameter.Key & " = NULL, "
                    Else
                        sqlString = sqlString & parameter.Key & " = '" & parameter.Value & "', "
                    End If

                End If

            Next

            'REMOVE THE LAST COMMA
            sqlString = sqlString.Remove(sqlString.LastIndexOf(","), 1)

            'Response.Write("SQL String ==>" & sqlString & "<==")

            Return sqlString

        Catch ex As ApplicationException
            Log.ErrorLog(_CLASS_NAME, "An error occurred in GetSearchResults() method.", ex)
            Throw ex
        End Try

    End Function

    Private Sub DisplayData()

        Dim sqlString As String = ""
        Dim listAdvancedSearch As List(Of Cdm.DataCore.Custom.AdvancedSearch) = Nothing

        Try

            'GET THE SQL QUERY
            sqlString = Me.CreateSqlQuery()

            Me.QueryStringHiddenField.Value = sqlString

            listAdvancedSearch = Cdm.DataCore.Custom.AdvancedSearch.Search(sqlString, Me._sortBy)

            Me.SearchResultsGridView.DataSource = listAdvancedSearch
            Me.SearchResultsGridView.DataBind()

            Me.SearchResultsCountLabel.Text = "Files returned: " & listAdvancedSearch.Count

        Catch ex As ApplicationException
            Log.ErrorLog(_CLASS_NAME, "An error occurred in DisplayData() method.", ex)
            Throw ex
        End Try

    End Sub

    Private Sub BindData()

        Try

            Me.SearchResultsGridView.DataSource = Cdm.DataCore.Custom.AdvancedSearch.Search(Me.QueryStringHiddenField.Value, Me.SortExpressionHiddenField.Value & " " & Me.SortDirectionHiddenField.Value)
            Me.SearchResultsGridView.DataBind()

        Catch ex As Exception
            Log.ErrorLog(_CLASS_NAME, "An error occurred in the BindData() method.", ex)
            Throw ex
        End Try

    End Sub

#End Region

#Region "EVENT HANDLERS"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim sqlString As String = ""

        Try

            If Not IsPostBack Then

                If Me.PreviousPage.ParameterCount > 0 Then

                    'STORE THE HASHTABLE INTO THE LOCAL MEMBER
                    Me._parameterCollection = PreviousPage.SqlParameters

                    Me.DisplayData()

                Else

                    Response.Redirect("AdvancedSearch.aspx?params=none", True)

                End If

            End If

        Catch ex As ApplicationException
            Log.ErrorLog(_CLASS_NAME, "An error occurred in Page_Load() method.", ex)
        End Try

    End Sub

    Protected Sub SearchResultsGridView_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles SearchResultsGridView.Sorting

        Try

            If Me.SortExpressionHiddenField.Value = e.SortExpression Then

                If Me.SortDirectionHiddenField.Value = "ASC" Or Me.SortDirectionHiddenField.Value = "" Then
                    Me.SortDirectionHiddenField.Value = "DESC"
                Else
                    Me.SortDirectionHiddenField.Value = "ASC"
                End If

            Else

                Me.SortExpressionHiddenField.Value = e.SortExpression
                Me.SortDirectionHiddenField.Value = "ASC"

            End If

            Me.BindData()

        Catch ex As Exception
            Log.ErrorLog(_CLASS_NAME, "An error occurred in the SearchResultsGridView_Sorting() method.", ex)
        End Try

    End Sub

    Protected Sub SearchResultsGridView_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles SearchResultsGridView.PageIndexChanging

        Try

            Me.SearchResultsGridView.PageIndex = e.NewPageIndex
            Me.BindData()

        Catch ex As Exception
            Log.ErrorLog(_CLASS_NAME, "An error occurred in the SearchResultsGridView_PageIndexChanging() method.", ex)
        End Try

    End Sub

#End Region

End Class
